package com.linian.pushforcampus.controller;

import com.linian.pushforcampus.domain.CampusActivity;
import com.linian.pushforcampus.domain.TotalPageNum;
import com.linian.pushforcampus.service.CampusActivityService;
import com.linian.pushforcampus.service.PushingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;



@Controller
public class PushingController {

    @Autowired
    private CampusActivityService campusActivityService;

    @Autowired
    private PushingService pushingService;



    @Autowired
    private GrabController grabController;


    public void getCampusActivityByJson() throws  IOException {
        List<CampusActivity> campusActivityListByJson = new ArrayList<>();

        //请求一次APi获取需要请求的次数,此时获取的是第一次请求的stirng

        String activityJsonByTokenByPageNum1 = campusActivityService.getActivityJsonByToken(1);
        //获取页数总数
        TotalPageNum totalPageNum = campusActivityService.pareApiJsonGetTotalPageNum(activityJsonByTokenByPageNum1);

        //解析第一页的json,并封装list对象,传入总list对象列表中
        List<CampusActivity> campusActivities = campusActivityService.parseActivitysApiJson(activityJsonByTokenByPageNum1);

        campusActivityListByJson.addAll(campusActivities);
        //默认从第一页请求，第一页已经请求完毕封装好对象，所以默认从第二页请求到总长度;
        //将所有请求到的json封装到一个list对象中
        for (int i = 2; i <= totalPageNum.getPageNum(); i++) {

            campusActivityListByJson.addAll(campusActivityService.parseActivitysApiJson(campusActivityService.getActivityJsonByToken(i)));
        }
        //查询数据库中记录的行数
        List<CampusActivity> campusActivitiesBySql = campusActivityService.queryAllActivity();
        if (campusActivitiesBySql.size() == 0) {
            //如果数据库没有数据,则先把所有获取的信息插入到数据库中
            for (CampusActivity campusActivity : campusActivityListByJson) {
                campusActivityService.inserActivity(campusActivity);
            }

        } else {

            //获取最终需要推送的活动
            List<CampusActivity> campusActivitiesResult = campusActivityService.judgmentEncapsulation(campusActivitiesBySql, campusActivityListByJson);

            if (campusActivitiesResult.size() > 0) {
                for (CampusActivity campusActivity : campusActivitiesResult) {

                    //如果是上架状态则进行抢课，否则不需要
                    if (campusActivity.getUpperandlowerStatus().equals("上架"))
                    {
                        grabController.grabActApply(campusActivity);
                    }


                }
                pushingService.wxpushingmsg(campusActivitiesResult);
            }

        }

    }

    /**
     * 更新推送活动信息
     */
    public void UpdateForPushing() throws IOException {
        // 创建一个空的CampusActivity列表
        List<CampusActivity> campusActivityListByJson = new ArrayList<>();

        // 获取第一页的活动Json
        String activityJsonByTokenByPageNum1 = campusActivityService.getActivityJsonByToken(1);

        // 获取总页数
        TotalPageNum totalPageNum = campusActivityService.pareApiJsonGetTotalPageNum(activityJsonByTokenByPageNum1);

        // 遍历所有页数
        for (int i = 0; i < totalPageNum.getPageNum(); i++) {
            // 获取当前页数的活动Json
            String activityJsonByTokenByPageNum = campusActivityService.getActivityJsonByToken(i);

            // 解析当前页数的活动数据并添加到列表中
            List<CampusActivity> campusActivities = campusActivityService.parseActivitysApiJson(activityJsonByTokenByPageNum);
            campusActivityListByJson.addAll(campusActivities);
        }

        // 查询所有活动数据
        List<CampusActivity> campusActivities = campusActivityService.queryAllActivity();

        // 遍历所有活动数据
        for (CampusActivity activity : campusActivities) {
            // 遍历推送活动列表
            for (int j = 0; j < campusActivityListByJson.size(); j++) {
                // 如果活动ID相等，则从列表中移除
                if (activity.getActivityId().equals(campusActivityListByJson.get(j).getActivityId())) {
                    campusActivityListByJson.remove(j);
                }
            }
        }

        // 如果推送活动列表不为空
        if (!campusActivityListByJson.isEmpty()) {
            // 遍历推送活动列表，并插入数据库
            for (CampusActivity campusActivity : campusActivityListByJson) {
                campusActivityService.inserActivity(campusActivity);
            }
            // 发送微信推送消息
            pushingService.wxpushingmsg(campusActivityListByJson);
        }
    }

}
